package com.timber.util;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.util.*;

import pv.jfcx.*;

import com.timber.util.*;

import javax.swing.event.*;
import com.borland.jbcl.layout.*;
import java.beans.*;

public class SearchableComboDialogWithEntity extends SearchableComboDialog
{
  public SearchableComboDialogWithEntity( JFrame parent, SearchableComboBox pSearchableComboBox, boolean mSelect )
  {
    super( parent, pSearchableComboBox, mSelect );
    try
    {
      myInit();
    }
    catch( Exception e )
    {
      e.printStackTrace();
    }
  }

  public SearchableComboDialogWithEntity( JDialog parent, SearchableComboBox pSearchableComboBox, boolean mSelect )
  {
    super( parent, pSearchableComboBox, mSelect );
    try
    {
      myInit();
    }
    catch( Exception e )
    {
      e.printStackTrace();
    }
  }

  private void myInit() throws Exception
  {
    pCenter.removeAll();

    allItemsTable = new SLTableView( new SearchableComboTableModelWithEntity( 0 ) );
    selectedItemsTable = new SLTableView( new SearchableComboTableModelWithEntity( 0 ) );
    allTm = ( SLTableModel )allItemsTable.getModel();
    selectedTm = ( SLTableModel )selectedItemsTable.getModel();
    searchableControl = new SearchableControl( allItemsTable );
    if( multiSelect )
      allItemsTable.getTable().getSelectionModel().setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
    else
      allItemsTable.getTable().getSelectionModel().setSelectionMode( ListSelectionModel.SINGLE_SELECTION );

    selectedItemsTable.getTable().getSelectionModel().setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );

    allTm.addTableModelListener( new TableModelListener()
      {
	public void tableChanged( TableModelEvent e )
	{
	  jLabel1.setText( "All Items:   " + allTm.getRowCount() );
	}
      } );

    selectedTm.addTableModelListener( new TableModelListener()
      {
	public void tableChanged( TableModelEvent e )
	{
	  jLabel2.setText( "Selected Items:   " + selectedTm.getRowCount() );
	}
      } );

    allItemsTable.setPreferredSize( new Dimension( 250, 200 ) );
    selectedItemsTable.setPreferredSize( new Dimension( 250, 200 ) );

    pCenter.add( jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
						,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    pCenter.add( searchableControl, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
							  ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    pCenter.add( allItemsTable, new GridBagConstraints(0, 2, 1, 1, 50.0, 1.0
						      ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    pCenter.add( pButtons, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
						 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    pCenter.add( jLabel2, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
						,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    pCenter.add( selectionCoB, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
						     ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    pCenter.add( selectedItemsTable, new GridBagConstraints(2, 2, 1,1, 50.0, 1.0
							   ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

  }

  void btnSelect_actionPerformed( ActionEvent e )
  {
    searchableControl.clear();
    if( !multiSelect && selectedItemsTable.getTable().getRowCount() > 0 )
    {
      java.awt.Toolkit.getDefaultToolkit().beep();
      return;
    }

    try
    {
      int[] arr = allItemsTable.getTable().getSelectedRows();
      SLDataObjectWithEntity item;
      boolean found;

      // loop through  selected accounts from "All Items"
      for( int i = 0; i < arr.length; i++ )
      {
        item = ( SLDataObjectWithEntity )allTm.itemAtTableRow( arr[ i ]);
        found = false;
        if( searchableComboBox == null || searchableComboBox.select_once )
        {
	  //check if item already exists in "Selected Items"
          for( int x = 0; x < selectedItemsTable.getTable().getRowCount(); x++ )
          {
            SLDataObjectWithEntity selectedItem = ( SLDataObjectWithEntity )selectedTm.itemAtTableRow( x );
            if( selectedItem.listString().equals( item.listString() ) &&
		selectedItem.getEntity().equals( item.getEntity() ) )
            {
              java.awt.Toolkit.getDefaultToolkit().beep();
              found = true;
              break;
            }
          }
        }

        if( !found )
          selectedTm.addDataRow( allTm.itemAtTableRow( arr[ i ] ) );
      }
    }
    catch( Exception ex )
    {
      JOptionPane.showMessageDialog( this, "Please select item" );
      ex.printStackTrace();
    }
  }

  public void setAllItems( Vector items )
  {
    allTm.setTableData( items );
  }

  public void setSelectedItems( Vector items )
  {
    selectedTm.setTableData( items );
  }

  public Vector getAllItems()
  {
    return allTm.getRowsVector();
  }

  public Vector getSelectedItems()
  {
    return selectedTm.getRowsVector();
  }
}
